//
//  RBGoodsDetailAttachPopVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/9/8.
//

#import "RBGoodsDetailAttachPopVC.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import <Masonry.h>
#import "RBThemeConfig.h"
#import <UIColor+YYAdd.h>
#import <UIView+YYAdd.h>
#import "RBPager.h"
#import "RanTool.h"
#import "RBTextField.h"
#import "RBTextView.h"
#import "RBScrollablePhotoContainer.h"

@interface RBGoodsDetailAttachPopVC ()

@property(nonatomic, strong) UITextField *dOrgNameF;
@property(nonatomic, strong) UITextField *dOrgAddressF;
@property(nonatomic, strong) UITextField *dNameF;
@property(nonatomic, strong) UITextField *dPhoneF;
@property(nonatomic, strong) UITextField *pNameF;
@property(nonatomic, strong) UITextField *pGenderF;
@property(nonatomic, strong) RBTextView *dNoteView;
@property(nonatomic, strong) RBScrollablePhotoContainer *photoContainerView;

@end

@implementation RBGoodsDetailAttachPopVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
}

#pragma mark - Private

- (void)setup
{
    self.contentView = UIView.new;
    self.contentView.backgroundColor = [RBThemeConfig bgGrayColor];
    [self.view addSubview:self.contentView];
    [self.contentView rbSetCorner:UIRectCornerTopLeft | UIRectCornerTopRight radius:Zoom(8)];
    
    [self.contentView view_addView:[RanTool labelWithFontSize:Zoom(13) weight:1 color:[RBThemeConfig textBlackColor] align:0 text:@"医疗信息填写"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(28) bottom:0 left:Zoom(13) right:CGFLOAT_MIN centerX:NO];
    
    UIImageView *closeBtn = [RanTool imgViewWithMode:0 radius:0 imgName:@"goodsDetailPramsClose"];
    [closeBtn extendResponseArea];
    [closeBtn rbAddTapGesWithTarget:self action:@selector(dismiss)];
    [self.contentView addSubview:closeBtn];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(Zoom(18));
        make.top.mas_equalTo(Zoom(22));
        make.right.mas_equalTo(-Zoom(16));
    }];
    
    [self.contentView view_addView:({
        UIView *view = [self textFieldItemWithPlaceholder:@"医疗结构名称"];
        self.dOrgNameF = (RBTextField *)[view viewWithTag:333];
        self.dOrgNameF.text = self.response.company_name;
        self.dOrgNameF.userInteractionEnabled = NO;
        view;
    }) withHeight:Zoom(36) width:Zoom(350) top:Zoom(20) bottom:0 left:0 right:0 centerX:1];
    
    [self.contentView view_addView:({
        UIView *view = [self textFieldItemWithPlaceholder:@"医疗结构地址"];
        self.dOrgAddressF = (RBTextField *)[view viewWithTag:333];
        NSString *addressString = [NSString stringWithFormat:@"%@%@%@%@", self.response.province, self.response.city, self.response.addr_detail, self.response.district];
        self.dOrgAddressF.text = addressString;
        self.dOrgAddressF.userInteractionEnabled = NO;
        view;
    }) withHeight:Zoom(36) width:Zoom(350) top:Zoom(12) bottom:0 left:0 right:0 centerX:1];
    
    [self.contentView view_addView:({
        UIView *view = UIView.new;
        [view h_view_addView:({
            UIView *view = [self textFieldItemWithPlaceholder:@"医生姓名"];
            self.dNameF = (RBTextField *)[view viewWithTag:333];
            self.dNameF.text = self.response.name;
            self.dNameF.userInteractionEnabled = NO;
            view;
        }) withHeight:CGFLOAT_MIN width:Zoom(170) top:0 bottom:0 left:0 right:0 centerY:0];
        [view h_view_addView:({
            UIView *view = [self textFieldItemWithPlaceholder:@"医生电话"];
            self.dPhoneF = (RBTextField *)[view viewWithTag:333];
            self.dPhoneF.text = self.response.mobile;
            self.dPhoneF.userInteractionEnabled = NO;
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(10) right:0 centerY:0];
        view;
    }) withHeight:Zoom(36) width:Zoom(350) top:Zoom(12) bottom:0 left:0 right:0 centerX:1];
    
    [self.contentView view_addView:({
        UIView *view = UIView.new;
        [view h_view_addView:({
            UIView *view = [self textFieldItemWithPlaceholder:@"患者姓名"];
            self.pNameF = (RBTextField *)[view viewWithTag:333];
            self.pNameF.text = self.pname;
            view;
        }) withHeight:CGFLOAT_MIN width:Zoom(170) top:0 bottom:0 left:0 right:0 centerY:0];
        [view h_view_addView:({
            UIView *view = [self textFieldItemWithPlaceholder:@"患者性别"];
            self.pGenderF = (RBTextField *)[view viewWithTag:333];
            self.pGenderF.text = self.pgender;
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(10) right:0 centerY:0];
        view;
    }) withHeight:Zoom(36) width:Zoom(350) top:Zoom(12) bottom:0 left:0 right:0 centerX:1];
    
    [self.contentView view_addView:({
        UIView *view = [self textViewItemWithPlaceholder:@"医生附言"];
        self.dNoteView = (RBTextView *)[view viewWithTag:333];
        self.dNoteView.text = self.dnote;
        view;
    }) withHeight:Zoom(107) width:Zoom(350) top:Zoom(12) bottom:0 left:0 right:0 centerX:1];
    
    [self.contentView view_addView:[RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:@"请填写真实的口腔信息"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(23) bottom:0 left:Zoom(13) right:0 centerX:0];
    
    [self.contentView view_addView:({
        RBScrollablePhotoContainer *view = [RBScrollablePhotoContainer viewWithMaxNum:9 itemWidth:Zoom(68) itemHeight:Zoom(68) itemPadding:Zoom(10) editable:YES];
        self.photoContainerView = view;
        view.bgColor = self.contentView.backgroundColor;
        if(self.pics.count)
        {
            [view addImages:self.pics];
        }
        view;
    }) withHeight:Zoom(68) width:CGFLOAT_MIN top:Zoom(13) bottom:0 left:Zoom(13) right:Zoom(13) centerX:0];
    
    [self.contentView view_addView:({
        UILabel *view = [RanTool labelWithFontSize:Zoom(15) weight:0 color:UIColor.whiteColor align:1 text:@"确定" bgColor:[RBThemeConfig buttonBlueColor] radius:Zoom(23)];
        [view rbAddTapGesWithTarget:self action:@selector(confirmBtnTapped)];
        view;
    }) withHeight:Zoom(46) width:Zoom(304) top:Zoom(25) bottom:MAX(kRBSafeBottomHeight, Zoom(28)) left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
    
    self.view.backgroundColor = UIColor.clearColor;
    self.contentView.width = kRBMainScreenWidth;
    self.contentView.height = self.contentView.esSize.height;
    self.contentView.left = 0;
    self.contentView.top = kRBMainScreenHeight;
}

- (void)show
{
    __weak typeof(self) weak_self = self;
    [UIView animateWithDuration:0.15 animations:^{
        weak_self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        weak_self.contentView.bottom = kRBMainScreenHeight;
    }];
}

- (void)dismiss
{
    __weak typeof(self) weak_self = self;
    [UIView animateWithDuration:0.15 animations:^{
        weak_self.view.backgroundColor = UIColor.clearColor;
        weak_self.contentView.top = kRBMainScreenHeight;
    } completion:^(BOOL finished) {
        [weak_self dismissViewControllerAnimated:NO completion:nil];
    }];
}


#pragma mark - Network

#pragma mark - Event

- (void)confirmBtnTapped
{
    if(!self.pNameF.text.length)
    {
        [self.view showTip:@"请填写患者姓名" completion:nil];
        return;
    }
    if(!self.pGenderF.text.length)
    {
        [self.view showTip:@"请填写患者性别" completion:nil];
        return;
    }
    if(self.completionBlock)
    {
        self.completionBlock(self.pNameF.text, self.pGenderF.text, self.dNoteView.text, self.photoContainerView.getImages);
    }
    [self dismissViewControllerAnimated:NO completion:nil];
}

#pragma mark - Setter

#pragma mark - Getter

- (UIView *)textFieldItemWithPlaceholder: (NSString *)placeholder
{
    UIView *view = UIView.new;
    view.backgroundColor = UIColor.whiteColor;
    [view rbSetCornerRadius:Zoom(5)];
    
    [view view_addView:({
        RBTextField *view = RBTextField.new;
        view.tag = 333;
        view.font = [UIFont systemFontOfSize:Zoom(13)];
        view.textColor = [RBThemeConfig textBlackColor];
        view.attributedPlaceholder = [RanTool attributeStringMaker:^(NSMutableArray *maker) {
            [maker addAttributeStringWithFont:[UIFont systemFontOfSize:Zoom(13)] color:[RBThemeConfig textGrayColor] string:placeholder];
        }];
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(14) right:Zoom(14) centerX:0];
    
    return view;
}

- (UIView *)textViewItemWithPlaceholder: (NSString *)placeholder
{
    UIView *view = UIView.new;
    view.backgroundColor = UIColor.whiteColor;
    [view rbSetCornerRadius:Zoom(5)];
    
    [view view_addView:({
        RBTextView *view = RBTextView.new;
        view.tag = 333;
        view.font = [UIFont systemFontOfSize:Zoom(13)];
        view.textColor = [RBThemeConfig textBlackColor];
        view.placeHolderColor = [RBThemeConfig textGrayColor];
        view.placeHolderFontSize = Zoom(13);
        view.placeHolder = placeholder;
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(14)-7 bottom:Zoom(14)-7 left:Zoom(14)-3 right:Zoom(14)-3 centerX:0];
    
    return view;
}


@end
